package com.qf.action;

import com.qf.dto.PageDto;
import com.qf.dto.TbComputerDto;
import com.qf.service.TbComputerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Controller
public class TbComputerAction {
    @Autowired
    @Qualifier("tbComputerService")
    private TbComputerService tbComputerService;

    //添加
    @RequestMapping("/addComputer.do")
    public String addComputer(HttpServletRequest request){
        String comModel=request.getParameter("comModel");
        String comGendate=request.getParameter("comGendate");
        String comPrice=request.getParameter("comPrice");
        String comDesc=request.getParameter("comDesc");
        TbComputerDto dto=new TbComputerDto();
        dto.setComModel(comModel);
        dto.setComGendate(comGendate);
        dto.setComPrice(Double.parseDouble(comPrice));
        dto.setComDesc(comDesc);
        try {
            tbComputerService.addComputer(dto);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "forward:/listComputer.do";
    }

    //删除
    @RequestMapping("/delComputer.do")
    public String delComputer(Long[] ids){
        for (Long id : ids) {
           tbComputerService.delComputer(id);
        }
        return "forward:/listComputer.do";
    }

    //修改
    @RequestMapping("/updateComputer.do")
    public ModelAndView updateComputer(TbComputerDto dto){
        ModelAndView modelAndView=new ModelAndView();
        try {
            tbComputerService.updateComputer(dto);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        modelAndView.setViewName("forward:/listComputer.do");
        return  modelAndView;
    }

    //查询单个：详情页detsilcomputer.jsp
    @RequestMapping("/detailComputer.do")
    public ModelAndView detailComputer(Long id){
        ModelAndView modelAndView=new ModelAndView();
        TbComputerDto dto = tbComputerService.findById(id);
        modelAndView.addObject("dto",dto);
        modelAndView.setViewName("detailcomputer");
        return modelAndView;
    }

    //查询分页:页面显示
    @RequestMapping("/listComputer.do")
    public ModelAndView listComputer(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
       int currentPage=1;
        //得到当前页
        String page=request.getParameter("page");
        if (StringUtils.isEmpty(page)){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(page);
        }
        if (currentPage<=1){
            currentPage=1;
        }
        PageDto pageDto=new PageDto();
        //将当前页传到service
        pageDto.setCurrentPage(currentPage);
        //获取service传过来的分页查询数据：
        List<TbComputerDto> bypage=tbComputerService.findBypage(pageDto);
        //获取总页数
        int maxPage=pageDto.getMaxPage();
        if (currentPage>maxPage){
            currentPage=maxPage;
        }
        modelAndView.addObject("bypage",bypage);
        modelAndView.addObject("currentPage",currentPage);
        modelAndView.addObject("maxPage",maxPage);
        modelAndView.setViewName("listComputer");
        return modelAndView;
    }
}
